package com.tramy.cnpush;

import android.util.Log;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.module.annotations.ReactModule;
import com.heytap.msp.push.HeytapPushManager;
import com.heytap.msp.push.callback.ICallBackResultService;

@ReactModule(name = OPPOPushModule.NAME)
public class OPPOPushModule extends ReactContextBaseJavaModule {

    public static final String NAME = "OPPOPush";

    private static class ResCallback implements ICallBackResultService {

        private final Callback callback;

        public ResCallback(Callback callback) {
            this.callback = callback;
        }

        @Override
        public void onRegister(int responseCode, String registerID) {
            if (responseCode == 0) {
                callback.invoke(registerID);
            }else {
                callback.invoke("");
            }
        }

        @Override
        public void onUnRegister(int responseCode) {

        }

        @Override
        public void onSetPushTime(int responseCode, String pushTime) {

        }

        @Override
        public void onGetPushStatus(int responseCode, int status) {

        }

        @Override
        public void onGetNotificationStatus(int responseCode, int status) {

        }

        @Override
        public void onError(int code, String msg) {
            Log.e("OppoPush", code + "," + msg);
        }
    }

    public OPPOPushModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    @NonNull
    public String getName() {
        return NAME;
    }

    @ReactMethod
    public void init(boolean isDebug,
                     Promise promise) {
        HeytapPushManager.init(
            getReactApplicationContext().getApplicationContext(),
            isDebug);
        promise.resolve("");
    }

    @ReactMethod
    public void isSupportPush(Promise promise) {
        boolean isSupport = HeytapPushManager.isSupportPush(
            getReactApplicationContext().getApplicationContext());
        promise.resolve(isSupport);
    }

    @ReactMethod
    public void requestNotificationPermission() {
        HeytapPushManager.requestNotificationPermission();
    }

    @ReactMethod
    public void openNotificationSettings() {
        HeytapPushManager.openNotificationSettings();
    }

    @ReactMethod
    public void register(String appKey,
                         String appSecret,
                         Callback callback) {
        HeytapPushManager.register(
            getReactApplicationContext().getApplicationContext(),
            appKey,
            appSecret,
            new ResCallback(callback));
    }

    @ReactMethod
    public void unRegister() {
        HeytapPushManager.unRegister();
    }

}
